package com.zjf.carpark.controller;


import com.zjf.carpark.annotation.MyLogAnno;
import com.zjf.carpark.entities.ICcard;
import com.zjf.carpark.entities.Owner;
import com.zjf.carpark.service.impl.ICcardServiceImpl;
import com.zjf.carpark.service.impl.OwnerServiceImpl;
import com.zjf.carpark.service.impl.ParkPlaceServiceImpl;
import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.vo.ICcardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

import static com.zjf.carpark.utils.CodeAndMessage.*;

/**
 * @description:IC卡相关接口
 * @author: 张建飞
 * @createDate: 2020/1/2
 * @version: 1.0
 */
@CrossOrigin
@Controller
@RequestMapping("ICCard")
public class ICcardController {

    @Autowired
    ICcardServiceImpl iCcardService;

    @Autowired
    OwnerServiceImpl ownerService;

    @Autowired
    ParkPlaceServiceImpl parkPlaceService;

    ResponseData responseData;

    @RequestMapping("add")
    @ResponseBody
    @MyLogAnno("新增IC卡")
    public ResponseData add(ICcard iCcard) {
        Integer save = iCcardService.save(iCcard);
        parkPlaceService.updateByNum(iCcard.getParkPlaceNum(), iCcard.getCarNum());
        if (save != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 通过id查询IC卡
     *
     * @param icCardId
     * @return
     */
    @RequestMapping("selectById")
    @ResponseBody
    public ResponseData selectById(Integer icCardId) {

        ICcard iCcard = iCcardService.selectById(icCardId);
        if (iCcard != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 通过用户Id查询用户是否存在
     *
     * @param ownerId
     * @return
     */
    @RequestMapping("selectByOwnerId")
    @ResponseBody
    public ResponseData selectByOwnerId(Integer ownerId) {

        Owner owner = ownerService.selectById(ownerId);
        if (owner != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 查询所有ic卡
     *
     * @param iCcardVO
     * @return
     */
    @RequestMapping("selectAllByMsg")
    @ResponseBody
    public ResponseData selectAllByMsg(ICcardVO iCcardVO) {
        List<ICcard> iCcards = iCcardService.selectByMsg(iCcardVO);
        Integer count = iCcardService.totalCount(iCcardVO);
        if (iCcards != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("iCcards", iCcards);
            responseData.setCount(count);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }


    /**
     * 删除IC卡
     *
     * @param icCardId
     * @return
     */
    @RequestMapping("deleteById")
    @ResponseBody
    @MyLogAnno("删除IC卡")
    public ResponseData deleteById(Integer icCardId) {
        Integer integer = iCcardService.deleteById(icCardId);
        if (integer != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 通过用户id查询ic卡
     *
     * @param data
     * @return
     */
    @RequestMapping("selectICCardByOwnerId")
    @ResponseBody
    public ResponseData selectICCardByOwnerId(@RequestBody Map<String, Integer> data) {
        Integer ownerId = data.get("ownerId");
        ICcard iCcard = iCcardService.selectICcardByOwnerId(ownerId);
        if (iCcard != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("iCcard", iCcard);
        }
        return responseData;
    }
}
